package com.yanqu.road.dao.impl.activity.feast;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.feast.data.CrossFeastPvpPlaceData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFeastPvpPlaceDataDaoImpl extends TempDataObjectDao<CrossFeastPvpPlaceData> {

    public static String getCrossFeastPvpPlaceDataSql = "select * from `t_c_feast_pvp_place_data` where `activityId`=?";

    public Map<Integer, CrossFeastPvpPlaceData> getCrossFeastPvpPlaceDataMap(int activityId) {
        Map<Integer, CrossFeastPvpPlaceData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFeastPvpPlaceDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFeastPvpPlaceData parse = parse(rs);
                map.put(parse.getPlaceId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossFeastPvpPlaceDataSql = "insert into `t_c_feast_pvp_place_data`(activityId,placeId,updateTime,firstUser) values (?,?,?,?)";

    @Override
    protected boolean addData(CrossFeastPvpPlaceData data) {

        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFirstUser()));
        if (getDb().executeNoneQuery(insertCrossFeastPvpPlaceDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossFeastPvpPlaceDataSql = "update `t_c_feast_pvp_place_data` set updateTime=?,firstUser=? where activityId=? and placeId=?";

    @Override
    protected boolean updateData(CrossFeastPvpPlaceData data) {

        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFirstUser()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        if (getDb().executeNoneQuery(updateCrossFeastPvpPlaceDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossFeastPvpPlaceData parseData(ResultSet rs) throws SQLException {
        CrossFeastPvpPlaceData data = new CrossFeastPvpPlaceData();
        data.setActivityId(rs.getInt("activityId"));
        data.setPlaceId(rs.getInt("placeId"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setFirstUser(rs.getLong("firstUser"));
        return data;
    }
}
